package com.tensquare.base.controller;
/*
 *ClassName:LabelController
 *Description:标签控制层
 *@author:可爱的大鸭子
 */

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.xml.bind.ValidationEvent;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin        //用于解决跨域调用
@RequestMapping("/label")
public class LabelController {
    @Autowired
    private LabelService labelService;

    /**
     * 查询全部列表
     * @return
     */
    @RequestMapping(method = RequestMethod.GET) //method指定提交的方法
    public Result findAll(){
        return new Result(true, StatusCode.OK,"查询成功", labelService.findAll() );
    }

    /**
     * 根据ID查询标签
     * @param id
     * @return
     */
    @RequestMapping(value="/{id}",method = RequestMethod.GET)   //value指定url
    public Result findById(@PathVariable String id){            //@PathVariable获取参数值，value看情况指明
        return new Result(true,StatusCode.OK,"查询成功",labelService.findById(id));
    }

    /**
     * 增加标签
     * @param label
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add( @RequestBody Label label){
        int flag=labelService.add(label);
        if(flag==0) return new Result(false,StatusCode.ERROR,"该标签已存在");
        return new Result(true,StatusCode.OK,"增加成功");
    }

    /**
     * 修改标签
     * @param label
     * @return
     */
    @RequestMapping(value="/{id}" ,method = RequestMethod.PUT)
    public Result update(@RequestBody Label label, @PathVariable String id){
        label.setId(id);
        int flag=labelService.update(label);
        if(flag==0) return new Result(false,StatusCode.ERROR,"该标签已存在");
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除标签
     * @param id
     * @return
     */
    @RequestMapping(value="/{id}" ,method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable String id){
        labelService.deleteById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 根据条件查询
     * @param searchMap
     * @return
     */
    @RequestMapping(value="/search",method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap){
        return new Result(true,StatusCode.OK,"查询成功",labelService.findSearch(searchMap));
    }

    /**
     * 分页查询条件
     * @param label 前端传递过来的查询条件
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value="/search/{page}/{size}",method = RequestMethod.POST)
    public Result pageQuery(@RequestBody Label label,@PathVariable int page,@PathVariable int size){
        Page<Label> pageData=labelService.pageQuery(page,size,label);
        PageResult<Label> pageResult=new PageResult<>(pageData.getTotalElements(),pageData.getContent());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 推荐标签列表
     * @return
     */
    @RequestMapping(value = "/toplist",method = RequestMethod.GET)
    public Result findTopList(){
        List<Label> list=labelService.findTopList();
        return new Result(true,StatusCode.OK,"查询成功",list);
    }

    /**
     * 有效标签列表
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result findList(){
        List<Label> list=labelService.findList();
        return new Result(true,StatusCode.OK,"查询成功",list);
    }

    /*热门标签列表*/
    @RequestMapping(value = "/hotlist",method = RequestMethod.GET)
    public Result findhotlist(){
        List<Label> list=labelService.findhotlist();
        return new Result(true,StatusCode.OK,"查询成功",list);
    }

    //返回我关注的标签列表
    @RequestMapping(value = "/myLabels/{me}",method = RequestMethod.GET)
    public Result getMyLabels(@PathVariable String me){
        List<String> list=labelService.getMyLabels(me);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }
}
